/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.entity.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.primefaces.event.SelectEvent;

@Named
@ViewScoped
public class SearchCalendarBean implements Serializable {

    @EJB
    UserManager um;
    
    @PersistenceContext
    EntityManager enm;
    
    private String username;
    
    public SearchCalendarBean() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    /**
     * Allows to search the calendar of another user and redirects to the right page
     * @return the next web page to navigate to, if you are not looking for your calendar page neither the calendar of a unknown user
     */
    public String searchAnotherCalendar() {
        //no username searched = stay in the page
        if(username == null) {
           return null;
        }
        try {
            User user = um.findUserByUsername(username);
            //the username searched is yours = stay in the page
            if(username.equalsIgnoreCase(um.getLoggedUser().getUsername())) {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("You are looking for yourself (?!)",  ""));
                return null;
            } else {
                //the calendar searched is public = redirect to username's page
                if(user.getCalendarPublic()) {
                    return "otherCalendar?faces-redirect=true&amp;username=" + username;
                } else {
                    //the calendar searched is private = stay in the page
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, new FacesMessage(username+" has a private calendar. You cannot see it!",  ""));
                    return null;
                }
            }
        } catch(EJBException e) {
            //the username searched is unknown = stay in the page
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("The user doesn't exist",  "") );
        return null;
       }
    }
    
    /**
     * Helps searching other people, suggesting their username
     * @param user username (or part of it) you are looking for
     * @return a list of usernames that you could be looking for
     */
    public List<String> completeText(String user) {
        List<String> results = new ArrayList<>();
        List<String> users = (List<String>) enm.createQuery("SELECT u.username FROM User u WHERE u.username LIKE CONCAT('%', :username, '%')")
                .setParameter("username", user).getResultList(); 
        for(String s: users) {
            if(!(s.equals(um.getLoggedUser().getUsername())))
                results.add(s);
        }
        return results;
    }
    
    /**
     * Handle the "username select" events, saving the selected username in the bean.
     * @param event username selected
     */
    public void onItemSelect(SelectEvent event) {
        username = event.getObject().toString();
    }   
}